#include "Character.h"
#include "Map.h"
Character::Character()
{
}

Character::~Character()
{
}

void Character::Load()
{
	mSprite.Load("pikachu_2.png");
}

void Character::Unload()
{
	mSprite.Unload();
}

void Character::Update(float deltaTime,const Map& map)
{
	//speed of the movement
	const float kSpeed = 200.0f; 
	float displacementX = 0.0f;
	float displacementY = 0.0f;
	float boundaryPixel = 1.0f;
	//movement control
	if(Input_IsKeyDown(Keys::RIGHT))
	{ 
		displacementX = kSpeed * deltaTime;
	}
	else if(Input_IsKeyDown(Keys::LEFT))
	{
		displacementX = -kSpeed * deltaTime;
	}//horisontal

	SRect boundingBox = GetBoundingBox() + SVector2(displacementX, 0.0f);//(x,y)
	if(displacementX > 0.0f)//check the phantom box collision
	{
		//check right
		SLineSegment segment = boundingBox.GetRightSegment();//getRight
		SRect collisionBox;
		if(map.TestCollision(segment,collisionBox))
		{
			//subtracting the col box with the bounding box get the sub it with the displacement(arrow)
			displacementX -= boundingBox.max.x - collisionBox.min.x;
			mPosition.x += displacementX-boundaryPixel;
		}
		else
		{
			mPosition.x += displacementX;
		}
	}
	else if(displacementX < 0.0f)
	{
		//Todo: check left
		SLineSegment segment = boundingBox.GetLeftSegment();
		SRect collisionBox;
		if(map.TestCollision(segment, collisionBox))
		{
			displacementX -= boundingBox.min.x - collisionBox.max.x;
			mPosition.x += (int)displacementX+boundaryPixel;
		}
		else
		{
			mPosition.x += (int)displacementX;
		}
	}
	


	if(Input_IsKeyDown(Keys::UP))
	{
		displacementY = -kSpeed * deltaTime;
	}
	else if(Input_IsKeyDown(Keys::DOWN))
	{
		displacementY = kSpeed * deltaTime;
	}//vertical
	//check up and down
	boundingBox = GetBoundingBox() + SVector2(0.0f, displacementY);
	if(displacementY < 0.0f)
	{
		//check up
		SLineSegment segment = boundingBox.GetTopSegment();
		SRect collisionBox;
		if(map.TestCollision(segment, collisionBox))
		{
			displacementY -= boundingBox.min.y - collisionBox.max.y;
			mPosition.y += (displacementY+boundaryPixel);
		}
		else
		{
			mPosition.y += displacementY;
		}
	}
	else if(displacementY >  0.0f)
	{
		//check up
		SLineSegment segment = boundingBox.GetBottomSegment();
		SRect collisionBox;
		if(map.TestCollision(segment, collisionBox))
		{
			displacementY -= boundingBox.max.y - collisionBox.min.y;
			mPosition.y += (displacementY-boundaryPixel);
		}
		else
		{
			mPosition.y += displacementY;
		}
	}
	
}

void Character::Render(const SVector2& viewOffset)
{
	const int width = mSprite.GetWidth();
	const int height = mSprite.GetHeight();

	SVector2 offset(width*0.5f , height*0.5f);
	SVector2 renderPosition = mPosition - offset - viewOffset;

	mSprite.SetPosition(renderPosition);
	mSprite.Render();
}

SRect Character::GetBoundingBox() const
{
	SRect boundingBox;
	float temp(mSprite.GetWidth()/2.0f);
	boundingBox.min.x = mPosition.x - temp;
	boundingBox.max.x = mPosition.x + temp;
	temp = mSprite.GetHeight()/2.0f;
	boundingBox.min.y = mPosition.y - temp;
	boundingBox.max.y = mPosition.y + temp;

	return boundingBox;
}
